package com.mall.user.services;

import com.mall.user.IUserVerifyService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.entitys.UserVerify;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.UserVerifyRequest;
import com.mall.user.dto.UserVerifyResponse;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by Enzo Cotter on 2020/10/17.
 */
@Service
@Component
public class UserVerifyServiceImpl implements IUserVerifyService {

    @Autowired
    UserVerifyMapper userVerifyMapper;

    @Autowired
    MemberMapper memberMapper;

    /**
     * 激活用户
     * 1.根据uuid去查询userVerify表，获取对应的数据
     *      如果没有查到数据，那么uuid无效，直接返回激活失败
     *      如果查到数据
     * 2.将查到的数据的username同传进来的username进行比对
     *      比对失败，就返回激活失败
     *      比对成功，就更新该表相应数据的isverify字段
     * 3.将member表的isverify字段也进行更新
     * @param userVerifyRequest
     * @return
     */
    @Override
    public UserVerifyResponse verify(UserVerifyRequest userVerifyRequest) {
        UserVerifyResponse userVerifyResponse = new UserVerifyResponse();
        //判空
        userVerifyRequest.requestCheck();
        Example example = new Example(UserVerify.class);
        example.createCriteria().andEqualTo("uuid",userVerifyRequest.getUuid());
        List<UserVerify> userVerifies = userVerifyMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(userVerifies)){
            //没查到
            userVerifyResponse.setCode(SysRetCodeConstants.DATA_NOT_EXIST.getCode());
            userVerifyResponse.setMsg(SysRetCodeConstants.DATA_NOT_EXIST.getMessage());
            return userVerifyResponse;
        }
        //比较
        UserVerify userVerify = userVerifies.get(0);
        String userName = userVerifyRequest.getUserName();
        if(!userName.equals(userVerify.getUsername())){
            //如果查询到的username和传进来的username匹配不一致
            userVerifyResponse.setCode(SysRetCodeConstants.DATA_NOT_EXIST.getCode());
            userVerifyResponse.setMsg(SysRetCodeConstants.DATA_NOT_EXIST.getMessage());
            return userVerifyResponse;
        }
        //比对成功 改userverify的激活字段
        userVerify.setIsVerify("Y");
        int update = userVerifyMapper.updateByPrimaryKey(userVerify);
        if(update != 1){
            userVerifyResponse.setCode(SysRetCodeConstants.SYSTEM_ERROR.getCode());
            userVerifyResponse.setMsg(SysRetCodeConstants.SYSTEM_ERROR.getMessage());
            return userVerifyResponse;
        }
        //将member表的isverify字段也进行更新
        Example exampleMember = new Example(Member.class);
        exampleMember.createCriteria().andEqualTo("username",userVerifyRequest.getUserName());
        List<Member> members = memberMapper.selectByExample(exampleMember);
        if(CollectionUtils.isEmpty(members)){
            //没查到
            userVerifyResponse.setCode(SysRetCodeConstants.USER_INFOR_UPDATE_FAIL.getCode());
            userVerifyResponse.setMsg(SysRetCodeConstants.USER_INFOR_UPDATE_FAIL.getMessage());
            return userVerifyResponse;
        }

        //更新member表
        Member member = members.get(0);
        member.setIsVerified("Y");
        int update2 = memberMapper.updateByPrimaryKey(member);
        if(update2 != 1){
            userVerifyResponse.setCode(SysRetCodeConstants.USER_INFOR_UPDATE_FAIL.getCode());
            userVerifyResponse.setMsg(SysRetCodeConstants.USER_INFOR_UPDATE_FAIL.getMessage());
            return userVerifyResponse;
        }
        return userVerifyResponse;
    }
}
